using System;
using System.Collections;
using System.Collections.Generic;
using BasicScript.Component;
using Game.Core;
using Game.Main.Controller;
using Game.Main.Model;
using Game.Manager;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Game.Main.View
{
	public class MapView : BaseView
	{
		private SceneController clr;

		private Transform scrollArea;

		private List<ButtonObject> btns = new List<ButtonObject>();

		private Image mask;

		private Image sky;

		private ScrollRect scrollRect;

		private float leftBorad;

		private float rightBoard;

		private GameObject btnReset;

		private Transform btnResetIcon;

		private Image btnResetBG;

		private bool isInBtnResetArea;

		private bool isBtnResetAnimPlaying;

		private bool isBtnResetFillAnimPlaying;

		private float rotateTime;

		private float btnResetMinScale = 1f;

		private float btnResetMaxScale = 1.25f;

		private bool isSun;

		private Image water;

		private List<GameObject> tower1s = new List<GameObject>();

		private List<GameObject> tower2s = new List<GameObject>();

		private List<Image> shadows = new List<Image>();

		private List<Image> topLines = new List<Image>();

		private List<Image> lines = new List<Image>();

		private Image light1;

		private Image light2;

		private AudioPlayer resetAudioPlayer;

		public MapView(SceneController clr)
			: base("prefabs/ui/scene/mapview", Singleton<UIManager>.Instance.uiLayer)
		{
			viewName = "MapView";
			this.clr = clr;
		}

		protected override void OnLoaded()
		{
			scrollRect = base.transform.Find("ScrollView").GetComponent<ScrollRect>();
			if (Singleton<SceneModel>.Instance.mapViewScrollX == -2.1474836E+09f)
			{
				scrollRect.horizontalNormalizedPosition = 0.5f;
			}
			else
			{
				scrollRect.horizontalNormalizedPosition = Singleton<SceneModel>.Instance.mapViewScrollX;
			}
			scrollRect.onValueChanged.AddListener(OnScrollRectValueChange);
			scrollArea = base.transform.Find("ScrollView/Area");
			water = scrollArea.Find("Water").GetComponent<Image>();
			Transform transform = scrollArea.Find("Shadow");
			int childCount = transform.childCount;
			for (int i = 0; i < childCount; i++)
			{
				shadows.Add(transform.GetChild(i).GetComponent<Image>());
			}
			Transform transform2 = scrollArea.Find("Tower");
			childCount = transform2.childCount;
			for (int j = 0; j < childCount; j++)
			{
				Transform child = transform2.GetChild(j);
				if (child.name.Contains("A"))
				{
					tower1s.Add(child.gameObject);
				}
				else
				{
					tower2s.Add(child.gameObject);
				}
			}
			Transform transform3 = scrollArea.Find("Line");
			childCount = transform3.childCount;
			for (int k = 0; k < childCount; k++)
			{
				Transform child2 = transform3.GetChild(k);
				if (child2.name.StartsWith("1"))
				{
					topLines.Add(child2.GetComponent<Image>());
				}
				else
				{
					lines.Add(child2.GetComponent<Image>());
				}
			}
			light1 = scrollArea.Find("Light1").GetComponent<Image>();
			light2 = scrollArea.Find("Light2").GetComponent<Image>();
			GameObject gameObject = scrollArea.Find("BtnSceneA/Image").gameObject;
			AddButtonScript(gameObject);
			GameObject gameObject2 = scrollArea.Find("BtnSceneB/Image").gameObject;
			AddButtonScript(gameObject2);
			GameObject gameObject3 = scrollArea.Find("BtnSceneC/Image").gameObject;
			AddButtonScript(gameObject3);
			GameObject gameObject4 = scrollArea.Find("BtnSceneD/Image").gameObject;
			AddButtonScript(gameObject4);
			GameObject gameObject5 = scrollArea.Find("BtnSceneE/Image").gameObject;
			AddButtonScript(gameObject5);
			GameObject gameObject6 = scrollArea.Find("BtnSceneF/Image").gameObject;
			AddButtonScript(gameObject6);
			Transform[] componentsInChildren = scrollArea.GetComponentsInChildren<Transform>();
			for (int l = 0; l < componentsInChildren.Length; l++)
			{
				if (componentsInChildren[l].name.StartsWith("Character"))
				{
					componentsInChildren[l].gameObject.AddComponent<MapViewCharacter>();
				}
				else if (componentsInChildren[l].name.StartsWith("Cars"))
				{
					componentsInChildren[l].gameObject.AddComponent<MapViewCarManager>();
				}
				else if (componentsInChildren[l].name.StartsWith("Cat"))
				{
					componentsInChildren[l].gameObject.AddComponent<MapViewCat>();
				}
				else if (componentsInChildren[l].name.StartsWith("Clound"))
				{
					componentsInChildren[l].gameObject.AddComponent<MapViewClound>();
				}
			}
			btnReset = GetChild("BtnReset").gameObject;
			btnResetIcon = GetChild("BtnReset/Icon");
			btnResetBG = GetChild("BtnReset/FillBG").GetComponent<Image>();
			UIEventSystem.Get(btnReset).AddEventListener(EventTriggerType.PointerDown, OnBtnResetDown);
			UIEventSystem.Get(btnReset).AddEventListener(EventTriggerType.PointerUp, OnBtnResetUp);
			UIEventSystem.Get(btnReset).AddEventListener(EventTriggerType.PointerExit, OnBtnResetExit);
			GameObject btnLight = scrollArea.Find("BtnLight").gameObject;
			if (Singleton<SaveManager>.Instance.IsMapViewLampOpen)
			{
				btnLight.transform.Find("Image").gameObject.SetActive(true);
			}
			UIEventSystem.Get(btnLight).AddEventListener(EventTriggerType.PointerClick, delegate
			{
				GameObject gameObject9 = btnLight.transform.Find("Image").gameObject;
				gameObject9.SetActive(!gameObject9.activeSelf);
				Singleton<AudioManager>.Instance.PlaySound("sound_lamp_open_close");
				Singleton<SaveManager>.Instance.IsMapViewLampOpen = gameObject9.activeSelf;
			});
			GameObject gameObject7 = GetChild("BtnPurchase").gameObject;
			ButtonObject btnPurchaseScript = gameObject7.AddComponent<ButtonObject>();
			btnPurchaseScript.InitParams(1.25f, 1.25f, 1f, 1f);
			UIEventSystem.Get(gameObject7).AddEventListener(EventTriggerType.PointerDown, delegate
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				btnPurchaseScript.ZoomIn();
			});
			UIEventSystem.Get(gameObject7).AddEventListener(EventTriggerType.PointerExit, delegate
			{
				btnPurchaseScript.ZoomOut();
			});
			UIEventSystem.Get(gameObject7).AddEventListener(EventTriggerType.PointerUp, delegate
			{
				btnPurchaseScript.ZoomOut();
			});
			UIEventSystem.Get(gameObject7).AddEventListener(EventTriggerType.PointerClick, delegate
			{
				clr.OCPurchaseView(true, true);
			});
			GameObject gameObject8 = GetChild("BtnBack").gameObject;
			ButtonObject btnBackScript = gameObject8.AddComponent<ButtonObject>();
			btnBackScript.InitParams(1.25f, 1.25f, 1f, 1f);
			UIEventSystem.Get(gameObject8).AddEventListener(EventTriggerType.PointerDown, delegate
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				btnBackScript.ZoomIn();
			});
			UIEventSystem.Get(gameObject8).AddEventListener(EventTriggerType.PointerExit, delegate
			{
				btnBackScript.ZoomOut();
			});
			UIEventSystem.Get(gameObject8).AddEventListener(EventTriggerType.PointerUp, delegate
			{
				btnBackScript.ZoomOut();
			});
			UIEventSystem.Get(gameObject8).AddEventListener(EventTriggerType.PointerClick, delegate
			{
				clr.ChangeScene(SceneType.MAIN_VIEW);
			});
			mask = GetChild("Mask").GetComponent<Image>();
			sky = GetChild("Sky").GetComponent<Image>();
			int hour = DateTime.Now.Hour;
			if (hour < 7 || hour > 18)
			{
				IsSunVisibled(false);
			}
			else
			{
				IsSunVisibled(true);
			}
			float num = (float)Screen.width / (float)Screen.height;
			leftBorad = -0.05f * num;
			rightBoard = 1f + 0.106f * num;
			scrollArea.Find("CableCar").gameObject.AddComponent<MapViewCableCar>();
			Singleton<AudioManager>.Instance.PlayMusic("music_main_scene");
		}

		private void IsSunVisibled(bool val)
		{
			isSun = val;
			if (val)
			{
				sky.color = new Color32(125, 174, 244, byte.MaxValue);
				water.color = new Color32(148, 235, byte.MaxValue, byte.MaxValue);
				light1.color = new Color32(128, 179, 245, byte.MaxValue);
				light2.color = new Color32(134, 186, 245, byte.MaxValue);
				for (int i = 0; i < shadows.Count; i++)
				{
					shadows[i].color = new Color32(156, 207, 249, byte.MaxValue);
				}
				for (int j = 0; j < tower1s.Count; j++)
				{
					tower1s[j].SetActive(true);
				}
				for (int k = 0; k < tower2s.Count; k++)
				{
					tower2s[k].SetActive(false);
				}
				for (int l = 0; l < topLines.Count; l++)
				{
					topLines[l].color = new Color32(86, 130, 175, byte.MaxValue);
				}
				for (int m = 0; m < lines.Count; m++)
				{
					lines[m].color = new Color32(111, 161, 232, byte.MaxValue);
				}
				GameObject gameObject = scrollArea.Find("Sun").gameObject;
				gameObject.AddComponent<MapViewSunMoon>();
				gameObject.SetActive(true);
				scrollArea.Find("Moon").gameObject.SetActive(false);
			}
			else
			{
				sky.color = new Color32(34, 37, 91, byte.MaxValue);
				water.color = new Color32(108, 150, byte.MaxValue, byte.MaxValue);
				light1.color = new Color32(35, 39, 97, byte.MaxValue);
				light2.color = new Color32(61, 76, 123, byte.MaxValue);
				for (int n = 0; n < shadows.Count; n++)
				{
					shadows[n].color = new Color32(89, 88, 137, byte.MaxValue);
				}
				for (int num = 0; num < tower1s.Count; num++)
				{
					tower1s[num].SetActive(false);
				}
				for (int num2 = 0; num2 < tower2s.Count; num2++)
				{
					tower2s[num2].SetActive(true);
				}
				for (int num3 = 0; num3 < topLines.Count; num3++)
				{
					topLines[num3].color = new Color32(86, 86, 132, byte.MaxValue);
				}
				for (int num4 = 0; num4 < lines.Count; num4++)
				{
					lines[num4].color = new Color32(61, 61, 119, byte.MaxValue);
				}
				GameObject gameObject2 = scrollArea.Find("Moon").gameObject;
				gameObject2.AddComponent<MapViewSunMoon>();
				gameObject2.SetActive(true);
				scrollArea.Find("Sun").gameObject.SetActive(false);
			}
		}

		public void OnScrollRectValueChange(Vector2 val)
		{
			if (val.x < leftBorad)
			{
				scrollRect.horizontalNormalizedPosition = leftBorad;
			}
			else if (val.x > rightBoard)
			{
				scrollRect.horizontalNormalizedPosition = rightBoard;
			}
		}

		private void AddButtonScript(GameObject btnObj)
		{
			ButtonObject btnScript = btnObj.transform.parent.gameObject.AddComponent<ButtonObject>();
			btnScript.InitParams(1.01f, 0.95f, 1f, 1f);
			btns.Add(btnScript);
			Button component = btnObj.GetComponent<Button>();
			UIEventSystem.Get(btnObj).AddEventListener(EventTriggerType.BeginDrag, delegate(BaseEventData evt)
			{
				scrollRect.OnBeginDrag((PointerEventData)evt);
			});
			UIEventSystem.Get(btnObj).AddEventListener(EventTriggerType.Drag, delegate(BaseEventData evt)
			{
				scrollRect.OnDrag((PointerEventData)evt);
			});
			UIEventSystem.Get(btnObj).AddEventListener(EventTriggerType.EndDrag, delegate(BaseEventData evt)
			{
				scrollRect.OnEndDrag((PointerEventData)evt);
			});
			UIEventSystem.Get(btnObj).AddEventListener(EventTriggerType.PointerDown, delegate
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				btnScript.ZoomIn();
			});
			UIEventSystem.Get(btnObj).AddEventListener(EventTriggerType.PointerExit, delegate
			{
				btnScript.ZoomOut();
			});
			UIEventSystem.Get(btnObj).AddEventListener(EventTriggerType.PointerUp, delegate
			{
				btnScript.ZoomOut();
			});
			UIEventSystem.Get(btnObj).AddEventListener(EventTriggerType.PointerClick, delegate
			{
				if (btnObj.transform.parent.name.Equals("BtnSceneA"))
				{
					clr.ChangeScene(SceneType.SCENE_6);
				}
				else if (btnObj.transform.parent.name.Equals("BtnSceneB"))
				{
					clr.ChangeScene(SceneType.SCENE_2);
				}
				else if (btnObj.transform.parent.name.Equals("BtnSceneC"))
				{
					clr.ChangeScene(SceneType.SCENE_1);
				}
				else if (btnObj.transform.parent.name.Equals("BtnSceneD"))
				{
					clr.ChangeScene(SceneType.SCENE_3);
				}
				else if (btnObj.transform.parent.name.Equals("BtnSceneE"))
				{
					clr.ChangeScene(SceneType.SCENE_4);
				}
				else if (btnObj.transform.parent.name.Equals("BtnSceneF"))
				{
					clr.ChangeScene(SceneType.SCENE_5);
				}
			});
		}

		private void OnBtnResetClick(BaseEventData evt)
		{
			clr.ClearArchive();
		}

		private void OnBtnResetDown(BaseEventData evt)
		{
			if (!isBtnResetFillAnimPlaying)
			{
				isInBtnResetArea = true;
				if (resetAudioPlayer == null)
				{
					resetAudioPlayer = Singleton<AudioManager>.Instance.CreateAudioPlayer(transform, false);
				}
				Singleton<AudioManager>.Instance.PlayAudio("sound_btn_reset", false, resetAudioPlayer);
				Client.Instance.StartCoroutine(PlayBtnResetZoomInAnim());
				Client.Instance.StartCoroutine(PlayFillResetBtnAnim());
			}
		}

		private void OnBtnResetUp(BaseEventData evt)
		{
			if (isInBtnResetArea)
			{
				isInBtnResetArea = false;
				ExitBtnReset();
			}
		}

		private void OnBtnResetExit(BaseEventData evt)
		{
			if (isInBtnResetArea)
			{
				isInBtnResetArea = false;
				ExitBtnReset();
			}
		}

		private void ExitBtnReset()
		{
			Singleton<AudioManager>.Instance.StopAudio(resetAudioPlayer);
			Singleton<AudioManager>.Instance.PlayAudio("sound_btn_reset_2", false, resetAudioPlayer);
			Client.Instance.StartCoroutine(PlayBtnResetZoomOutAnim());
			Client.Instance.StartCoroutine(PlayUnfillResetBtnAnim());
		}

		private IEnumerator PlayFillResetBtnAnim()
		{
			isBtnResetFillAnimPlaying = true;
			yield return new WaitForEndOfFrame();
			float curFill = btnResetBG.fillAmount;
			float curRotation = btnResetIcon.localRotation.eulerAngles.z;
			while (transform != null && isBtnResetFillAnimPlaying)
			{
				btnResetBG.fillAmount = curFill;
				curFill += Time.deltaTime;
				curRotation += Time.deltaTime * 360f;
				if (curFill >= 1f)
				{
					btnResetBG.fillAmount = 1f;
					btnResetIcon.eulerAngles = new Vector3(0f, 0f, 0f);
					clr.ClearArchive();
					isInBtnResetArea = false;
					Client.Instance.StartCoroutine(PlayResetSuccessAnim());
					bool isZoomIn = true;
					float curScale = btnReset.transform.localScale.x;
					float targetScale = curScale * 1.2f;
					while (isZoomIn)
					{
						curScale += Time.deltaTime * 2f;
						if (curScale >= targetScale)
						{
							SetGameObjectScale(btnReset, targetScale);
							isZoomIn = false;
						}
						else
						{
							SetGameObjectScale(btnReset, curScale);
						}
						yield return new WaitForEndOfFrame();
					}
					ExitBtnReset();
					yield return new WaitForSeconds(0.1f);
					while (isBtnResetFillAnimPlaying)
					{
						yield return new WaitForEndOfFrame();
					}
					rotateTime = 0f;
					while (rotateTime < 2f)
					{
						btnResetIcon.eulerAngles = new Vector3(0f, 0f, (0f - rotateTime) * 360f);
						rotateTime += Time.deltaTime;
						if (rotateTime >= 2f)
						{
							btnResetIcon.eulerAngles = new Vector3(0f, 0f, 0f);
						}
						yield return new WaitForEndOfFrame();
					}
					mask.gameObject.SetActive(false);
					break;
				}
				btnResetBG.fillAmount = curFill;
				btnResetIcon.eulerAngles = new Vector3(0f, 0f, curRotation);
				yield return new WaitForEndOfFrame();
			}
		}

		private IEnumerator PlayResetSuccessAnim()
		{
			float timer = 0f;
			bool isPlaying = true;
			mask.color = new Color(1f, 1f, 1f, 0f);
			mask.gameObject.SetActive(true);
			int stage = 0;
			while (isPlaying)
			{
				switch (stage)
				{
				case 0:
					timer += Time.deltaTime;
					if (timer >= 1f)
					{
						stage = 1;
						timer = 0f;
						mask.color = new Color(1f, 1f, 1f, 1f);
					}
					else
					{
						mask.color = new Color(1f, 1f, 1f, timer);
					}
					break;
				case 1:
					timer += Time.deltaTime;
					if (timer >= 0.2f)
					{
						timer = 0f;
						stage = 2;
					}
					break;
				default:
					timer += Time.deltaTime;
					if (timer >= 1f)
					{
						mask.color = new Color(1f, 1f, 1f, 0f);
						stage = 0;
						isPlaying = false;
						mask.gameObject.SetActive(false);
					}
					else
					{
						mask.color = new Color(1f, 1f, 1f, 1f - timer);
					}
					break;
				}
				yield return new WaitForEndOfFrame();
			}
		}

		private IEnumerator PlayUnfillResetBtnAnim()
		{
			isBtnResetFillAnimPlaying = false;
			yield return new WaitForEndOfFrame();
			isBtnResetFillAnimPlaying = true;
			float curFill = btnResetBG.fillAmount;
			float curRotation = btnResetIcon.localRotation.eulerAngles.z;
			while (transform != null && isBtnResetFillAnimPlaying)
			{
				btnResetBG.fillAmount = curFill;
				curFill -= Time.deltaTime * 2f;
				curRotation -= Time.deltaTime * 720f;
				if (curFill <= 0f)
				{
					btnResetBG.fillAmount = 0f;
					btnResetIcon.eulerAngles = new Vector3(0f, 0f, 0f);
					isBtnResetFillAnimPlaying = false;
				}
				else
				{
					btnResetBG.fillAmount = curFill;
					btnResetIcon.eulerAngles = new Vector3(0f, 0f, curRotation);
				}
				yield return new WaitForEndOfFrame();
			}
		}

		private IEnumerator PlayBtnResetZoomInAnim()
		{
			isBtnResetAnimPlaying = false;
			yield return new WaitForEndOfFrame();
			isBtnResetAnimPlaying = true;
			float curScale = btnReset.transform.localScale.x;
			while (transform != null && isBtnResetAnimPlaying)
			{
				curScale += Time.deltaTime;
				if (curScale >= btnResetMaxScale)
				{
					SetGameObjectScale(btnReset, btnResetMaxScale);
					isBtnResetAnimPlaying = false;
					break;
				}
				SetGameObjectScale(btnReset, curScale);
				yield return new WaitForEndOfFrame();
			}
		}

		private IEnumerator PlayBtnResetZoomOutAnim()
		{
			isBtnResetAnimPlaying = false;
			yield return new WaitForEndOfFrame();
			isBtnResetAnimPlaying = true;
			float curScale = btnReset.transform.localScale.x;
			while (transform != null && isBtnResetAnimPlaying)
			{
				curScale -= Time.deltaTime;
				if (curScale <= btnResetMinScale)
				{
					SetGameObjectScale(btnReset, btnResetMinScale);
					isBtnResetAnimPlaying = false;
					Singleton<AudioManager>.Instance.StopAudio(resetAudioPlayer);
					break;
				}
				SetGameObjectScale(btnReset, curScale);
				yield return new WaitForEndOfFrame();
			}
		}

		private void SetGameObjectScale(GameObject go, float val)
		{
			go.transform.localScale = new Vector3(val, val, val);
		}

		public override void Destroy()
		{
            //base.Destroy();
            this.Close(false);
            Singleton<SceneModel>.Instance.mapViewScrollX = scrollRect.horizontalNormalizedPosition;
		}
	}
}
